package com.hoshiibuy.brandcategory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.brandcategory.entity.GoodsBrand;
import com.hoshiibuy.brandcategory.entity.param.GoodsBrandQueryParam;
import com.hoshiibuy.brandcategory.entity.param.GoodsBrandUpdateParam;
import com.hoshiibuy.brandcategory.entity.param.GoodsBrandAddParam;
import com.hoshiibuy.brandcategory.service.GoodsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商品品牌表 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-04-30
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/brandcategory/goods-brand")
@Api(value = "商品类目及品牌管理", tags = {"商品品牌表管理"})
public class GoodsBrandController {
    @Autowired
    private GoodsBrandService goodsBrandService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询商品品牌表
    */
    @ApiOperation(value = "分页查询商品品牌表")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid GoodsBrandQueryParam param) {
        try {
            log.info("分页查询商品品牌表");
            QueryWrapper<GoodsBrand> queryWrapper = new QueryWrapper<GoodsBrand>();
            queryWrapper.eq(param.getCompanyid() != null, "companyid", param.getCompanyid());
            queryWrapper.eq(StringUtils.isNotBlank(param.getChineseName()), "chinese_name", param.getChineseName());
            queryWrapper.eq(StringUtils.isNotBlank(param.getEnglishName()), "english_name", param.getEnglishName());
            queryWrapper.eq(param.getOrdered() != null, "ordered", param.getOrdered());
            queryWrapper.eq(StringUtils.isNotBlank(param.getLogo()), "logo", param.getLogo());
            queryWrapper.eq(StringUtils.isNotBlank(param.getTrademarkNo()), "trademark_no", param.getTrademarkNo());
            queryWrapper.eq(StringUtils.isNotBlank(param.getRegistrationNo()), "registration_no", param.getRegistrationNo());
            queryWrapper.eq(param.getProducing() != null, "producing", param.getProducing());
            queryWrapper.eq(StringUtils.isNotBlank(param.getAddress()), "address", param.getAddress());
            queryWrapper.eq(StringUtils.isNotBlank(param.getRegistrationApplyno()), "registration_applyno", param.getRegistrationApplyno());
            queryWrapper.eq(StringUtils.isNotBlank(param.getRegistrationOffer()), "registration_offer", param.getRegistrationOffer());
            queryWrapper.eq(StringUtils.isNotBlank(param.getBelongto()), "belongto", param.getBelongto());
            queryWrapper.eq(StringUtils.isNotBlank(param.getRemark()), "remark", param.getRemark());
            queryWrapper.eq(param.getStatus() != null, "status", param.getStatus());
            IPage<GoodsBrand> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(GoodsBrand.class));
            return Response.data(goodsBrandService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询商品品牌表异常", e);
            return Response.failed("分页查询商品品牌表异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询商品品牌表
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商品品牌表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(goodsBrandService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增商品品牌表")
    public Response save(@RequestBody @Valid GoodsBrandAddParam param) {
        try {
            GoodsBrand data = BeanUtils.covertType(param, GoodsBrand.class);
            return Response.data(goodsBrandService.save(data));
        } catch (ServiceException e) {
            log.error("新增商品品牌表异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增商品品牌表异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改商品品牌表
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改商品品牌表")
    public Response update(@RequestBody @Valid GoodsBrandUpdateParam param) {
        try {
            GoodsBrand data = BeanUtils.covertType(param, GoodsBrand.class);
            return Response.data(goodsBrandService.updateById(data));
        }catch (ServiceException e) {
            log.error("修改商品品牌表异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改商品品牌表异常", e);
            return Response.failed();
        }
    }
}
